<template>
	<Table ref="tableRef" :url="URL_HISTORYFAILURELIST" :columns="data.columns" :handles="data.handles"
		:toolbar="data.toolbar" :query="data.query" :params="{ unit: true }" listKey="dataList"></Table>
</template>

<script lang="ts" setup name="historyFailureList">
	import Table from '@/components/table/index.vue';
	import {
		URL_HISTORYFAILURELIST,
		EXPORT_HISTORYFAILURE,
	} from '@/service/apis/alarm';
	import { exportFile } from '@/utils/utils';
	const router = useRouter();
	const tableRef = ref();

	const emit = defineEmits(['get-update']);
	onActivated(() => {
		emit('get-update', (flag : boolean) => {
			if (flag) {
				tableRef.value?.getData();
			}
		});
	});
	const data = reactive({
		pickDate: null as any,
		columns: [
			{
				prop: 'createTime',
				label: '故障时间',
			},
			{
				prop: 'recoveryTime',
				label: '恢复时间',
			},
			{
				prop: 'connectorCode',
				label: '终端编码',
			},
			{
				prop: 'equipmentType',
				label: '电桩类型',
				template: (row : any, index : number) => {
					return row.izUser
						? `分体桩`
						: `一体桩`;
				},
			},
			{
				prop: 'failureReason',
				label: '故障原因',
			},
			{
				prop: 'failureBit',
				label: '故障码',
			},
			{
				prop: 'remark',
				label: '备注',
			},
		],
		handles: [

		],
		toolbar: [
			{
				label: '导出',
				permission: 'alarmNotificationRecordList.export',
				icon: 'icon-export',
				type: 'primary',
				plain: true,
				handle: (row : any) => {
					console.log('导出---', row);
					exportFile(EXPORT_HISTORYFAILURE, '历史故障', tableRef.value?.getForm());
				},
			},
		],
		query: [
			{
				label: '终端编码',
				prop: 'connectorCode',
				comName: 'input',
				props: {
					placeholder: '请输入终端编码',
				},
			},
			{
				label: '故障码',
				prop: 'failureBit',
				comName: 'input',
				props: {
					placeholder: '请输入故障码',
				},
			},
			{
				label: '电桩类型',
				prop: 'equipmentType',
				comName: 'select',
				props: {
					placeholder: '请选择电桩类型',
					options: [
						{ label: "一体桩", type: "alarmStatus", value: "0" },
						{ label: "分体桩", type: "alarmStatus", value: "1" }] as any,
				},

			},
			{
				label: '故障时间',
				prop: 'createData',
				comName: 'date',
				keys: ['createTimeBegin', 'createTimeEnd'],
				props: {
					type: 'daterange',
					calendarChange: (pick : any) => {
						let hasSelectDate = pick !== null && pick.length > 0;
						data.pickDate = hasSelectDate ? pick[0] : null;
					},
					change: (time : any) => {
						if (!time) {
							data.pickDate = null;
						}
					},
					disabledDate: (time : any) : any => {
						if (data.pickDate) {
							const checkTime = data.pickDate.getTime();
							const startTime = checkTime - 62 * 86400 * 1000; // 前面限制62天
							const endTime = checkTime + 62 * 86400 * 1000; // 后面限制62天
							return time.getTime() > endTime || time.getTime() < startTime;
						} else {
							return false;
						}
					},
				},
			},
			{
				label: '恢复时间',
				prop: 'recoveryData',
				comName: 'date',
				keys: ['recoveryTimeBegin', 'recoveryTimeEnd'],
				props: {
					type: 'daterange',
					calendarChange: (pick : any) => {
						let hasSelectDate = pick !== null && pick.length > 0;
						data.pickDate = hasSelectDate ? pick[0] : null;
					},
					change: (time : any) => {
						if (!time) {
							data.pickDate = null;
						}
					},
					disabledDate: (time : any) : any => {
						if (data.pickDate) {
							const checkTime = data.pickDate.getTime();
							const startTime = checkTime - 62 * 86400 * 1000; // 前面限制62天
							const endTime = checkTime + 62 * 86400 * 1000; // 后面限制62天
							return time.getTime() > endTime || time.getTime() < startTime;
						} else {
							return false;
						}
					},
				},
			},
		],
	});
</script>